﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Microsoft.Practices.Unity;
using System.Windows.Input;

namespace VpPrism.Modules.ModuleTwo.ViewModels
{
  /// <summary>
  /// ViewModel du ModuleTwo
  /// </summary>
  public class ModuleTwoMainVM: IModuleTwoMainVM, INotifyPropertyChanged
  {
    #region Fields

    private readonly IUnityContainer container;

    #endregion

    #region Commands
    #region SendMessageToModuleOneCommand
    private ICommand _SendMessageToModuleOneCommand;
    /// <summary>
    /// Commande pour envoyer un message au ModuleOne
    /// </summary>
    public ICommand SendMessageToModuleOneCommand
    {
      get
      {
        if (this._SendMessageToModuleOneCommand == null)
          this._SendMessageToModuleOneCommand = new VpPrism.Common.Command.RelayCommand<object>((item) => this.SendMessageToModuleOne(item), (item) => this.CanSendMessageToModuleOne());

        return this._SendMessageToModuleOneCommand;
      }
    }
    /// <summary>
    /// Fonction d'envoi de message via le EventAggregator au ModuleOne
    /// </summary>
    /// <param name="parameter"></param>
    public void SendMessageToModuleOne(object parameter)
    {
      if (!(parameter is String)) return;
      VpPrism.Events.User.UserActionEventArgs userAction = new VpPrism.Events.User.UserActionEventArgs(VpPrism.Common.Enums.UserAction.SendMessage, parameter as String);
      this.container.Resolve<Microsoft.Practices.Composite.Events.IEventAggregator>().GetEvent<VpPrism.Events.User.UserActionEvent>().Publish(userAction);
    }
    private bool CanSendMessageToModuleOne()
    {
      return true;
    }
    #endregion
    #endregion

    #region IModuleTwoMainVM Members
    /// <summary>
    /// Vue principale
    /// </summary>
    public VpPrism.Modules.ModuleTwo.Views.IModuleTwoMainV MainView {get; private set;}

    #endregion

    /// <summary>
    /// Constructeur. Les paramètres sont pré-remplis par le UnityContainer.
    /// </summary>
    /// <param name="container"></param>
    /// <param name="mainView"></param>
    public ModuleTwoMainVM(IUnityContainer container, VpPrism.Modules.ModuleTwo.Views.IModuleTwoMainV mainView)
    {
      this.container = container;
      this.MainView = mainView;
      this.MainView.SetModel(this);
    }

    #region INotifyPropertyChanged Members
    /// <summary>
    /// Implémentation de INotifyPropertyChanged.
    /// </summary>
    public event PropertyChangedEventHandler PropertyChanged;

    /// <summary>
    /// Fonction de Raise de l'évènement PropertyChanged.
    /// </summary>
    /// <param name="PropertyName"></param>
    protected void OnPropertyChanged(string PropertyName)
    {
      if (PropertyChanged != null)
      {
        PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
      }
    }

    #endregion
  }
}
